--[[
	绑定账号界面
	2016_06_23 Ravioyla
]]
local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local utils = appdf.req(appdf.EXTERNAL_SRC .. "Utils")

local BindingLayer = class("BindingLayer", function(scene)
		local BindingLayer = display.newLayer()--cc.c4b(0, 0, 0, 255)
    return BindingLayer
end)

local QueryDialog = require("app.views.layer.other.QueryDialog")
local NotifyMgr = appdf.req(appdf.EXTERNAL_SRC .. "NotifyMgr")
BindingLayer.btn_exit = 10
BindingLayer.btn_toregisiter = 11
BindingLayer.btn_sendcode = 12
BindingLayer.changeView = 13
-- 进入场景而且过渡动画结束时候触发。
function BindingLayer:onEnterTransitionFinish()
    return self
end

-- 退出场景而且开始过渡动画时候触发。
function BindingLayer:onExitTransitionStart()
    return self
end

--打开注册界面 VerityCodejson 获取验证码的json
-- function utils.UpAndRigesterAccountView(parent,VerityCodejson)
function BindingLayer:ctor(parent,isTips)
	self._scene = parent
	local btcallback = function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect()
			self:onButtonClickedEvent(ref:getTag(),ref)
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
			
		end
	end
	
	local scheduler = cc.Director:getInstance():getScheduler()
	self.scheduler = scheduler
			self.schedulerID = nil
			
			local rootLayer, csbNode = ExternalFun.loadRootCSB("Logon/bandPhoneLayer.csb")
			-- ExternalFun.FixTopUI(csbNode)
			-- ExternalFun.FixScaleUI(csbNode,true)
			local bg2 =  csbNode:getChildByName("likesong")
			self.bg = csbNode:getChildByName("meitiansong")
			ExternalFun.FixScaleUI(bg2,true)
			self.bg2 = bg2
			local registerBg = csbNode:getChildByName("registerBg")
			ExternalFun.FixScaleUI(registerBg,true)
			self.registerBg = registerBg
			registerBg:setVisible(false)
			bg2:setVisible(true)
			self.bg:setVisible(true)
			local zhezhao = csbNode:getChildByName("Panel_1")
			local tempTextField = registerBg:getChildByName("TextField_phone")
			--账号输入
			local edit_Account = ccui.EditBox:create(tempTextField:getContentSize(), ccui.Scale9Sprite:create(""))
			self.edit_Account = edit_Account
			:setPosition(tempTextField:getPosition())
			:setFontName("fonts/round_body.ttf")
			:setPlaceholderFontName("fonts/round_body.ttf")
			:setFontSize(tempTextField:getFontSize())
			:setMaxLength(11)
			:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
			:setPlaceHolder("请输入手机号")
			:addTo(registerBg)
			tempTextField:removeFromParent()
			tempTextField = registerBg:getChildByName("TextField_code")
			--验证码
			local  edit_code = ccui.EditBox:create(tempTextField:getContentSize(), ccui.Scale9Sprite:create(""))
			self.edit_code = edit_code
			:setPosition(tempTextField:getPosition())
			:setFontName("fonts/round_body.ttf")
			:setPlaceholderFontName("fonts/round_body.ttf")
			:setFontSize(tempTextField:getFontSize())
		--	:setFontColor(cc.c3b(204,172,139))
		--	:setPlaceholderFontColor(cc.c3b(204,172,139))
			:setMaxLength(6)
			--	:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)
			:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
			:setPlaceHolder("请输入验证码")
			:addTo(registerBg)
			
			tempTextField:removeFromParent()
			tempTextField = registerBg:getChildByName("TextField_pwd")
			--输入密码
			local  edit_pwd1 = ccui.EditBox:create(tempTextField:getContentSize(), ccui.Scale9Sprite:create(""))
			self.edit_pwd1 = edit_pwd1
			:setPosition(tempTextField:getPosition())
			:setFontName("fonts/round_body.ttf")
		--	:setFontColor(cc.c3b(204,172,139))
		--	:setPlaceholderFontColor(cc.c3b(204,172,139))
			:setPlaceholderFontName("fonts/round_body.ttf")
			:setFontSize(tempTextField:getFontSize())
			 :setMaxLength(12)
			:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)
			--:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
			:setPlaceHolder("请输入密码")
			:addTo(registerBg)
			
			tempTextField:removeFromParent()
			tempTextField = registerBg:getChildByName("TextField_repwd")
			--确认密码
			local  edit_pwd2 = ccui.EditBox:create(tempTextField:getContentSize(), ccui.Scale9Sprite:create(""))
			self.edit_pwd2 = edit_pwd2
			:setPosition(tempTextField:getPosition()) 
		--	:setFontColor(cc.c3b(204,172,139))
		--	:setPlaceholderFontColor(cc.c3b(204,172,139))
			:setFontName("fonts/round_body.ttf")
			:setPlaceholderFontName("fonts/round_body.ttf")
			:setFontSize(tempTextField:getFontSize())
			:setMaxLength(12)
			:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)
			--:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
			:setPlaceHolder("确认密码")
			:addTo(registerBg)
			
			tempTextField:removeFromParent()
			--登录按钮
			local querenbt =   registerBg:getChildByName("Button_1_0")
			querenbt:setTag(BindingLayer.btn_toregisiter)
			querenbt:addTouchEventListener(btcallback)
			
			
			--Top返回
			
			local btn = registerBg:getChildByName("closebt")
			btn:setTag(BindingLayer.btn_exit)
			btn:addTouchEventListener(btcallback)
			
			btn = bg2:getChildByName("Button_1_0")
			btn:setTag(BindingLayer.btn_exit)
			btn:addTouchEventListener(btcallback)
			
			btn = self.bg:getChildByName("Button_1_0")
			btn:setTag(BindingLayer.btn_exit)
			btn:addTouchEventListener(btcallback)
			
			
			---发送验证码
			btn =registerBg:getChildByName("Button_1")
			btn:setTag(BindingLayer.btn_sendcode)
			btn:addTouchEventListener(btcallback)
				----升级提示
			
			bg2:setLocalZOrder(100)
			self.bg:setLocalZOrder(100)
				
			btn = bg2:getChildByName("Button_1")
			btn:setTag(BindingLayer.changeView)
			btn:addTouchEventListener(btcallback)
			btn = self.bg:getChildByName("Button_1")
			btn:setTag(BindingLayer.changeView)
			btn:addTouchEventListener(btcallback)
	

		rootLayer:addTo(self)
		self:setreggold()

		if isTips then
			self:onButtonClickedEvent(BindingLayer.changeView)
			ExternalFun.playOpenLayerAnimation( self.registerBg )
		else
			ExternalFun.playOpenLayerAnimation( bg2 )
		end
		
		
		--ExternalFun.playOpenLayerAnimation( bg2 )
end

function BindingLayer:setreggold()
	local money = 0
	if GlobalUserItem.channelconfig then
		money = GlobalUserItem.channelconfig.reggold or 0
	else
		money = 0
		GlobalUserItem.channelconfig = {}
	end

	local almsnum = GlobalUserItem.channelconfig.almsnum or 0
	local almscount = GlobalUserItem.channelconfig.almscount or 0
	local almslimit = GlobalUserItem.channelconfig.almslimit or 0
	almslimit = almslimit/appdf.bili
	if money == 0 then
		money = almsnum*almscount
		self.bg2:setVisible(false)
		self.bg:setVisible(true)
		money = money or 0
		money = money/appdf.bili
		self.bg:getChildByName("moneyAtlas"):setString(almslimit)
		self.bg:getChildByName("lingqu_atlas"):setString(money)
	else
		self.bg2:setVisible(true)
		self.bg:setVisible(false)
		money = money or 0
		money = money/appdf.bili
		local moneyAtlas = self.bg2:getChildByName("moneyAtlas")
			moneyAtlas:setString(money)
		local size = moneyAtlas:getContentSize()
		local px,py = moneyAtlas:getPosition()
		display.newSprite("Logon/givejinbi.png")
		:setAnchorPoint(0,0.5)
		:move(moneyAtlas:getPositionX()+size.width+5,moneyAtlas:getPositionY())
		:addTo(self.bg2)
		self.bg2:getChildByName("buzu_atlas"):setString(almslimit)
		self.bg2:getChildByName("lingqu_atlas"):setString(almsnum*almscount/appdf.bili)
	end

	
	
	

end
function BindingLayer:bindcb(recjson )

	showToast(self._scene,recjson.msg,2)
	if recjson.result == 1 then
		GlobalUserItem.szAccount = self.Account 
		GlobalUserItem.szPassword = self.pwd
		GlobalUserItem.showBindPanle = true
		GlobalUserItem.accounttype = 1
		
		if self.schedulerID then
			cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulerID)
		end
		

		GlobalUserItem.onSaveAccountConfig()
		self._scene.register_btn:removeFromParent()
		self:removeFromParent()
		
	end

end

--按钮事件
function BindingLayer:onButtonClickedEvent(tag,ref)

	if tag == BindingLayer.btn_exit then
		if self.schedulerID then
			cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulerID)
		end
		self:removeFromParent()
	elseif tag == BindingLayer.btn_toregisiter then
		local codeStr = string.gsub(self.edit_code:getText()," ","")
		local AccountStr = string.gsub(self.edit_Account:getText()," ","")
		
		local code1 = string.gsub(self.edit_pwd1:getText()," ","")
		local code2 = string.gsub(self.edit_pwd2:getText()," ","")
		
		if AccountStr == "" then
			showToast(self,"手机号不能为空或空格",1)
			return
		end
		
		if  string.len(AccountStr) ~= 11 then
			showToast(self,"手机号长度错误",1)
			return
		end
		local pos = string.find(AccountStr,"1")
		if  not pos or pos ~= 1 then
			showToast(self,"手机号码错误",1)
			return
		end

		if codeStr == "" or AccountStr == "" then
			showToast(self,"手机号或验证码不能为空或空格",1)
			return
		end

		if code1 == "" then
			self.edit_pwd1:setText("")
			self.edit_pwd2:setText("")
			showToast(self,"密码不能为空或空格",1)
			return
		end
		local slen = string.len(code1)
		if slen < 6 or  slen > 12  then
			showToast(self,"密码长度不能少于6位或多于12位",1)
			return
		end

		if code1 ~= code2 then
			showToast(self,"两次输入的密码不一致",1)
			return
		end

		self.pwd = md5(md5("&^axG2"..code1.."!!@##"))
		self.Account = AccountStr
		
		self._scene._LogonFrame:sendRegister(AccountStr,tonumber(codeStr) ,self.pwd,self.codeidx)
	elseif tag == BindingLayer.btn_sendcode then
		local codeStr = string.gsub(self.edit_code:getText()," ","")
		local AccountStr = string.gsub(self.edit_Account:getText()," ","")
		if  AccountStr == "" then
			showToast(self,"手机号不能为空",1)
			return
		end
		ref:setEnabled(false)
		--  ref:setTitleText("")
		local sec = 90
		ref:setTitleText("重发（"..sec..  "）")
		
		local cb = function(code,idx,verifycode,msg)
			self.codeidx = idx
			self.verifycode = verifycode
			showToast(self,msg or "发送失败",1)
		end
		local verityjson = {}
		verityjson.phone = AccountStr
		verityjson.userid = GlobalUserItem.dwUserID
		verityjson.channelid = appdf.channelId
		utils.getVerityCode(verityjson,cb )
		
		self.schedulerID = self.scheduler:scheduleScriptFunc(function ()
			if not tolua.isnull(ref) then
				sec = sec-1
				ref:setTitleText("重发（"..sec..  "）")
				if sec <= 0 then
					cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulerID)
					ref:setEnabled(true)
					ref:setTitleText("获取验证码")
				end
			end
		end,1,false)
		
	elseif tag == BindingLayer.changeView then
		self.registerBg:setVisible(true)
		self.bg2:setVisible(false)	
		self.bg:setVisible(false)


	end
end
return BindingLayer